package org.example.tx.acl;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class AccessControlList {

    @Getter
    @Setter
    @EqualsAndHashCode.Include
    private Integer id;
    @Getter
    @Setter
    @EqualsAndHashCode.Include
    private Integer version;

    private final List<AccessControlEntry> entries = new ArrayList<>();

    public void add(AccessControlEntry ace) {
        add(entries.size(), ace);
    }

    public void add(int position, AccessControlEntry ace) {
        ace.setAcl(this);
        entries.add(position, ace);
    }

    public void remove(int position) {
        AccessControlEntry ace = entries.remove(position);
        ace.setAcl(null);
    }

    @Override
    public String toString() {
        return "{%d,%d,%s}".formatted(id, version, entries);
    }
}
